//This is the implementation file of agp_
//

#include "agp.h"

//parse input commandline arguments
bool parseARG(int argc, char ** argv, string& agp_guard_filename, string& ply_filename);

//save agp_guards to a file
void save_agp_guard(const string& filename, const vector<agp_guard>& guards);

///////////////////////////////////////////////////////////////////////////////
agp_triangulation::agp_triangulation(agp_poly& _ply):ply(_ply)
{
    //TO DO: triangulate the polygon (ply)
    //you can use any triangulation packages 
    //or implement a triangulation method by
    //yourself
}

///////////////////////////////////////////////////////////////////////////////
void computeGuards(agp_triangulation& tris, vector<agp_guard>& guards)
{
    //TO DO: compute where the guards are using the triangulation
    //"tris", store the guards in "guards"
}

///////////////////////////////////////////////////////////////////////////////
int main(int argc, char ** argv)
{
    //--------------------------------------------------------------------------
    string guard_filename; //output filename
    string ply_filename;   //input filename

    //parse the argument
    if(!parseARG(argc,argv, guard_filename, ply_filename)){
        cerr<<"Usage: "<<argv[0]<<" *.guard *.poly "<<endl;
        return 1;
    }

    //create polygon from file
    agp_poly polygon(agp_poly::POUT); //an external poolygon chain
    read_poly(ply_filename,polygon); //defined in agp_util

    //compute triangulation here
    agp_triangulation tris(polygon);
    if(tris.triangles.empty()){
        cerr<<"! ERROR: Something is wrong in triangulation. No triangules!!"<<endl;
        return 1;
    }
    cout<<"- Find "<<tris.triangles.size()<<" triangle(s)"<<endl;

    //results
    vector<agp_guard> guards;  //you will have to compute where the guards are
    computeGuards(tris,guards);
    if(guards.empty()){
        cerr<<"! ERROR: No guards found?!"<<endl;
        return 1;
    }
    cout<<"- Find "<<guards.size()<<" guard(s)"<<endl;

    //save your results to a file
    save_agp_guard(guard_filename, guards);

    //everthing is fine
    return 0;
}

///////////////////////////////////////////////////////////////////////////////
bool parseARG
(int argc, char ** argv, string& guard_filename, string& ply_filename)
{
    if( argc<3 )
        return false;

    guard_filename=argv[1];
    ply_filename=argv[2];

    //check if everything is valid...
    if(ply_filename.empty()){
        cerr<<"! ERROR: No input polygon file"<<endl;
        return false;
    }

    if(guard_filename.empty()){
        cerr<<"! ERROR: No output agp_guard file"<<endl;
        return false;
    }

    //everything seems fine, output the input values
    cout<<"- Polygon fileanme:"<<ply_filename<<"\n"
        <<"- Guard fileanme:"<<guard_filename<<endl;

    return true;
}

///////////////////////////////////////////////////////////////////////
void save_agp_guard(const string& filename, const vector<agp_guard>& guards)
{
    //open file
    ofstream fout(filename.c_str());
    if( !fout.good() ){
        cerr<<"! ERROR: CANNOT open file : "<<filename<<endl;
        return;
    }
    int gsize=guards.size();
    fout<<gsize<<"\n"; //output agp_guard size
    for(int i=0;i<gsize;i++)
        fout<<guards[i].vertex->getID()<<"\n"; //output agp_guard vertex id
    fout.close();
}


